import axios from 'axios';
let uploadAuth, uploadAddress, videoId
let uploader = new AliyunUpload.Vod({
    //分片大小默认1M，不能小于100K
    partSize: 1048576,
    //并行上传分片个数，默认5
    parallel: 5,
    //网络原因失败时，重新上传次数，默认为3
    retryCount: 3,
    //网络原因失败时，重新上传间隔时间，默认为2秒
    retryDuration: 2,
    // 开始上传
    'onUploadstarted': function(uploadInfo) {
        log("onUploadStarted:" + uploadInfo.file.name + ", endpoint:" + uploadInfo.endpoint + ", bucket:" + uploadInfo.bucket + ", object:" + uploadInfo.object);
        //上传方式1, 需要根据uploadInfo.videoId是否有值，调用点播的不同接口获取uploadauth和uploadAddress，如果videoId有值，调用刷新视频上传凭证接口，否则调用创建视频上传凭证接口
        uploader.setUploadAuthAndAddress(uploadInfo, uploadAuth, uploadAddress, videoId);
        //上传方式2
        // uploader.setSTSToken(uploadInfo, accessKeyId, accessKeySecret,secretToken);
    },
    // 文件上传成功
    'onUploadSucceed': function(uploadInfo) {
        log("onUploadSucceed: " + uploadInfo.file.name + ", endpoint:" + uploadInfo.endpoint + ", bucket:" + uploadInfo.bucket + ", object:" + uploadInfo.object);
    },
    // 文件上传失败
    'onUploadFailed': function(uploadInfo, code, message) {
        log("onUploadFailed: file:" + uploadInfo.file.name + ",code:" + code + ", message:" + message);
    },
    // 文件上传进度，单位：字节
    'onUploadProgress': function(uploadInfo, totalSize, loadedPercent) {
        log("onUploadProgress:file:" + uploadInfo.file.name + ", fileSize:" + totalSize + ", percent:" + Math.ceil(loadedPercent * 100) + "%");
    },
    // 上传凭证超时
    'onUploadTokenExpired': function(uploadInfo) {
        console.log("onUploadTokenExpired");
        //上传方式1  实现时，根据uploadInfo.videoId调用刷新视频上传凭证接口重新获取UploadAuth
        // uploader.resumeUploadWithAuth(uploadAuth);
        // 上传方式2 实现时，从新获取STS临时账号用于恢复上传
        // uploader.resumeUploadWithSTSToken(accessKeyId, accessKeySecret, secretToken, expireTime);
    },
    //全部文件上传结束
    'onUploadEnd': function(uploadInfo) {
        log("onUploadEnd: uploaded all the files");
    }
});
export function getVideoUploadAuth(file) {
    let params = {
        video_title: file.name,
        video_url: file.path
    }
    axios.post('https://nc.yjsb18.xyz/aliapi/upload.php', params).then(res => {
            uploadAuth = res.uploadAuth
            uploadAddress = res.uploadAddress
            videoId = res.videoId
        })
        // return new Promise((resolve, reject) => {
        //     axios.post('https://nc.yjsb18.xyz/aliapi/upload.php', params)
        //         .then(response => {
        //             resolve(response);
        //         }, err => {
        //             reject(err)
        //         })
        // })
}

export let upload = uploader